增加了加解密

chengzhenyu vor 7 Jahren
Ursprung
Commit
94d4a0831f

+ 6 - 0
app/src/main/AndroidManifest.xml

@@ -122,6 +122,12 @@
122 122
             android:screenOrientation="portrait"/>
123 123
 
124 124
         <activity
125
+            android:name=".activities.EncryptActivity"
126
+            android:configChanges="keyboardHidden|orientation|screenSize"
127
+            android:label="@string/app_name"
128
+            android:screenOrientation="portrait"/>
129
+
130
+        <activity
125 131
             android:name=".upload.UploadActivity"
126 132
             android:configChanges="keyboardHidden|orientation|screenSize"
127 133
             android:label="@string/app_name"

+ 5 - 0
app/src/main/java/ai/pai/lensman/activities/AboutUsActivity.java

@@ -33,6 +33,7 @@ public class AboutUsActivity extends BaseActivity implements View.OnClickListene
33 33
         findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);
34 34
         findViewById(R.id.layout_about_declare).setOnClickListener(this);
35 35
         findViewById(R.id.layout_check_update).setOnClickListener(this);
36
+        findViewById(R.id.title_bar_middle_txt).setOnClickListener(this);
36 37
     }
37 38
 
38 39
     @Override
@@ -41,6 +42,10 @@ public class AboutUsActivity extends BaseActivity implements View.OnClickListene
41 42
             case R.id.title_bar_back_layout:
42 43
                 finish();
43 44
                 break;
45
+            case R.id.title_bar_middle_txt:
46
+                Intent encryptIntent = new Intent(this,EncryptActivity.class);
47
+                startActivity(encryptIntent);
48
+                break;
44 49
             case R.id.layout_about_declare:
45 50
                 Intent declareIntent = new Intent(this,WebViewActivity.class);
46 51
                 declareIntent.putExtra("url","http://pai.ai/page/user_agreement");

+ 81 - 0
app/src/main/java/ai/pai/lensman/activities/EncryptActivity.java

@@ -0,0 +1,81 @@
1
+package ai.pai.lensman.activities;
2
+
3
+import android.os.Bundle;
4
+import android.view.View;
5
+import android.widget.ImageView;
6
+import android.widget.TextView;
7
+import android.widget.Toast;
8
+
9
+import com.android.common.utils.LogHelper;
10
+import com.nostra13.universalimageloader.core.DisplayImageOptions;
11
+
12
+import ai.pai.lensman.R;
13
+import ai.pai.lensman.base.BaseActivity;
14
+import ai.pai.lensman.utils.EncryptUtils;
15
+import ai.pai.lensman.utils.ImageLoaderUtils;
16
+import ai.pai.lensman.utils.SystemUtils;
17
+
18
+
19
+public class EncryptActivity extends BaseActivity implements View.OnClickListener {
20
+
21
+    private ImageView image;
22
+    private String originImgFilePath = "/mnt/sdcard/demo.jpg";
23
+    private String encryptImgFilePath = "/mnt/sdcard/encrypt.jpg";
24
+    private String decryptImgFilePath = "/mnt/sdcard/decrypt.jpg";
25
+    private DisplayImageOptions options;
26
+
27
+    private String currentPath = "";
28
+
29
+    @Override
30
+    protected void onCreate(Bundle savedInstanceState) {
31
+        super.onCreate(savedInstanceState);
32
+        setContentView(R.layout.activity_encrypt);
33
+        SystemUtils.setImmerseLayout(this, findViewById(R.id.title_layout));
34
+        options = ImageLoaderUtils.getOptions(R.drawable.default_img);
35
+
36
+
37
+        ((TextView) findViewById(R.id.title_bar_middle_txt)).setText("加密");
38
+        findViewById(R.id.title_bar_back_layout).setOnClickListener(this);
39
+        findViewById(R.id.title_bar_option_layout).setVisibility(View.INVISIBLE);
40
+        findViewById(R.id.btn_encrypt).setOnClickListener(this);
41
+        findViewById(R.id.title_bar_back_layout).setOnClickListener(this);
42
+        image = (ImageView) findViewById(R.id.image_encrypt);
43
+        currentPath = originImgFilePath;
44
+        ImageLoaderUtils.displayLocalImage(currentPath, image, options);
45
+
46
+    }
47
+
48
+    @Override
49
+    public void onClick(View v) {
50
+        switch (v.getId()) {
51
+            case R.id.title_bar_back_layout:
52
+                finish();
53
+                break;
54
+            case R.id.btn_encrypt:
55
+                try {
56
+                    if (currentPath.equals(originImgFilePath) || currentPath.equals(decryptImgFilePath)) {
57
+                        LogHelper.d("czy", "开始加密");
58
+                        EncryptUtils.xorFile(originImgFilePath, encryptImgFilePath, "chengzhenyu".getBytes());
59
+                        LogHelper.d("czy", "加密成功");
60
+                        currentPath = encryptImgFilePath;
61
+                        ImageLoaderUtils.displayLocalImage(currentPath, image, options);
62
+                    } else {
63
+                        LogHelper.d("czy", "开始解密");
64
+                        EncryptUtils.xorFile(encryptImgFilePath, decryptImgFilePath, "chengzhenyu".getBytes());
65
+                        LogHelper.d("czy", "解密成功");
66
+                        currentPath = decryptImgFilePath;
67
+                        ImageLoaderUtils.displayLocalImage(currentPath, image, options);
68
+                    }
69
+                } catch (Exception e) {
70
+                    e.printStackTrace();
71
+                    LogHelper.d("czy", "加解密失败 " + e);
72
+                    Toast.makeText(this, "加解密失败", Toast.LENGTH_SHORT).show();
73
+                }
74
+                break;
75
+            default:
76
+                break;
77
+        }
78
+    }
79
+
80
+
81
+}

+ 58 - 0
app/src/main/java/ai/pai/lensman/utils/EncryptUtils.java

@@ -0,0 +1,58 @@
1
+package ai.pai.lensman.utils;
2
+
3
+import java.io.File;
4
+import java.io.FileInputStream;
5
+import java.io.FileOutputStream;
6
+import java.io.IOException;
7
+
8
+public class EncryptUtils {
9
+
10
+    public static void xorFile(String filename, String dest, byte[] password) throws IOException {
11
+        File destFile = new File(dest);
12
+        if(destFile.exists()){
13
+            destFile.delete();
14
+        }
15
+        destFile.createNewFile();
16
+        FileInputStream is = new FileInputStream(filename);
17
+        FileOutputStream os = new FileOutputStream(dest);
18
+
19
+        byte[] data = new byte[1024 * 4]; //4 KB buffer
20
+        int read = is.read(data), index = 0;
21
+        while (read != -1) {
22
+            for (int k = 0; k < read; k++) {
23
+                data[k] ^= password[index % password.length];
24
+                index++;
25
+            }
26
+            os.write(data, 0, read);
27
+            read = is.read(data);
28
+        }
29
+
30
+        os.flush();
31
+        os.close();
32
+        is.close();
33
+    }
34
+
35
+    public static void xorFile(String filename, String dest, byte[] password, long encryptByteCount) throws IOException {
36
+        FileInputStream is = new FileInputStream(filename);
37
+        FileOutputStream os = new FileOutputStream(dest);
38
+
39
+        byte[] data = new byte[1024 * 4]; //4 KB buffer
40
+        int read = is.read(data), index = 0, encryptCnt = 0;
41
+        while (read != -1) {
42
+            for (int k = 0; k < read; k++) {
43
+                if (encryptCnt < encryptByteCount) {
44
+                    data[k] ^= password[index % password.length];
45
+                    index++;
46
+                    encryptCnt++;
47
+                }
48
+            }
49
+            os.write(data, 0, read);
50
+            read = is.read(data);
51
+        }
52
+
53
+        os.flush();
54
+        os.close();
55
+        is.close();
56
+    }
57
+
58
+}

+ 32 - 0
app/src/main/res/layout/activity_encrypt.xml

@@ -0,0 +1,32 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+    android:layout_width="match_parent"
4
+    android:layout_height="match_parent"
5
+    android:background="@color/background_light_grey_color">
6
+
7
+    <RelativeLayout
8
+        android:id="@+id/title_layout"
9
+        android:layout_width="match_parent"
10
+        android:layout_height="wrap_content"
11
+        android:background="@color/colorPrimaryDark">
12
+        <include layout="@layout/title_bar_with_back_and_option" />
13
+    </RelativeLayout>
14
+
15
+    <ImageView
16
+        android:id="@+id/image_encrypt"
17
+        android:layout_width="match_parent"
18
+        android:layout_height="300dp"
19
+        android:scaleType="centerInside"
20
+        android:layout_below="@id/title_layout"/>
21
+
22
+    <Button
23
+        android:id="@+id/btn_encrypt"
24
+        android:layout_width="100dp"
25
+        android:layout_height="48dp"
26
+        android:layout_alignParentBottom="true"
27
+        android:layout_marginBottom="60dp"
28
+        android:layout_centerHorizontal="true"
29
+        android:gravity="center"/>
30
+
31
+
32
+</RelativeLayout>